﻿using System;
using System.Collections.Generic;

using System.Text;
using Entity;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Threading;
namespace Service
{
    public class Base_Pick_Service
    {
        Task _task;
        int currPage = 0;
        ToolStripProgressBar _planBar;
        ToolStripStatusLabel _lblPageIndex;
        public Base_Pick_Service(Task task, ToolStripProgressBar proPick, ToolStripStatusLabel lblPageIndex)
        {
            _task = task;
            _planBar = proPick;
            _lblPageIndex = lblPageIndex;
        }

        public List<Company> Pick()
        {
            List<Company> list = new List<Company>();
            //逐页采集
            for (int i = _task.Keyword.Page_From; i <= _task.Keyword.Page_To; i++)
            {
                currPage = i;
                _lblPageIndex.Text = "【" + _task.Keyword.Name + "】【" + currPage.ToString() + "】：";
                string url = Get_URL();
                string htmlStr = Common.RequestHelper.DoGet(url, Encoding.GetEncoding("gb2312"));
                List<Company> result_Page = Pick_51job.Pick_Page(_task,htmlStr, _planBar);
                list.AddRange(result_Page);
                DAL.SQLiteHelper.ExecuteNonQuery("update Keyword set Page_From = " + currPage.ToString() + " where Name = '" + _task.Keyword.Name + "'");
                //DAL.SQLiteHelper.ExecuteNonQuery("delete from Company as a where Name not in (select top 1 Name from Company where a.Name <> Name)");
            }
            return list;
        }

        private string Get_URL()
        {
            StringBuilder sb = new StringBuilder("http://search.51job.com/jobsearch/search_result.php?fromJs=1&jobarea=");
            sb.Append(_task.City.Code);
            sb.Append("&district=000000&funtype=0000&industrytype=00&issuedate=9&providesalary=99&keyword=");
            sb.Append(System.Web.HttpUtility.UrlEncode(_task.Keyword.Name).ToUpper());
            sb.Append("&keywordtype=2&curr_page=");
            sb.Append(currPage.ToString());
            sb.Append("&lang=c&stype=1&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=01&lonlat=0%2C0&radius=-1&ord_field=0&list_type=0&fromType=14");
            return sb.ToString();
        }

    }
}